package com.bootx.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.*;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class FileUploadUtils {

    public static void upload(String content,String path){
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "bootx-video";
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5tAy4RgkiwXmdj8jeCSK", "xK3mKmO6QurmjPtyO6DEkIu1KdqbMs");
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, new ByteArrayInputStream(content.getBytes()));
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }


    public static String uploadNet(String url,String path){
        if(StringUtils.isEmpty(url)){
            return null;
        }
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "bootx-video";
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5tAy4RgkiwXmdj8jeCSK", "xK3mKmO6QurmjPtyO6DEkIu1KdqbMs");
        try {
            InputStream inputStream = new URL(url).openStream();
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, inputStream);
            ossClient.putObject(putObjectRequest);
            return "https://bootx-video.oss-cn-hangzhou.aliyuncs.com/"+path;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    public static void delete(String objectName) throws Exception {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "bootx-video";
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5tAy4RgkiwXmdj8jeCSK", "xK3mKmO6QurmjPtyO6DEkIu1KdqbMs");
        try {
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static void list() throws Exception {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "bootx-video";
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5tAy4RgkiwXmdj8jeCSK", "xK3mKmO6QurmjPtyO6DEkIu1KdqbMs");
        int maxKeys = 200;
        try {
            String nextMarker = null;
            ObjectListing objectListing;
            do {
                objectListing = ossClient.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
                List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
                for (OSSObjectSummary s : sums) {
                    delete(s.getKey());
                    System.out.println("\t" + s.getKey());
                }
                nextMarker = objectListing.getNextMarker();

            } while (objectListing.isTruncated());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Boolean exist = exist("cover/1000229319840646.png");
        System.out.println(exist);
    }

    public static Boolean exist(String objectName) {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "bootx-video";
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5tAy4RgkiwXmdj8jeCSK", "xK3mKmO6QurmjPtyO6DEkIu1KdqbMs");
        try {
            return ossClient.doesObjectExist(bucketName, objectName);
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static String getPath(String cover) {
        return "https://bootx-video.oss-cn-hangzhou.aliyuncs.com/"+cover;
    }
}